home *** CD-ROM | disk | FTP | other *** search
/ 17 Bit Software 6: Level 6 / 17 Bit - Level 6 (1998)(Epic Marketing)[!].iso / quartz / q1082.dms / q1082.adf / src.lzh / Fig / graphics.c < prev    next >
C/C++ Source or Header  |  1991-07-18  |  3KB  |  97 lines

  1. /* 
  2.  *    FIG : Facility for Interactive Generation of figures
  3.  *
  4.  *    Copyright (c) 1985 by Supoj Sutanthavibul (supoj@sally.UTEXAS.EDU)
  5.  *    January 1985.
  6.  *    1st revision : March 1988
  7.  *
  8.  *    %W%    %G%
  9. */
  10. #include "fig.h"
  11. #include "resources.h"
  12. #include "paintop.h"
  13.  
  14. /****************************************************************************
  15.  
  16.     The following spline drawing routine is from 
  17.  
  18.     "An Algorithm for High-Speed Curve Generation" 
  19.     by George Merrill Chaikin,
  20.     Computer Graphics and Image Processing, 3, Academic Press, 
  21.     1974, 346-349.
  22.  
  23.     and
  24.  
  25.     "On Chaikin's Algorithm" by R. F. Riesenfeld,
  26.     Computer Graphics and Image Processing, 4, Academic Press, 
  27.     1975, 304-310.
  28.  
  29. *****************************************************************************/
  30.  
  31. #define        round(x)    ((int) (x + .5))
  32. #define        half(z1, z2)    ((z1+z2)/2.0)
  33. #define        THRESHOLD    5
  34.  
  35. /* iterative version */
  36. quadratic_spline(a1, b1, a2, b2, a3, b3, a4, b4, op)
  37. float    a1, b1, a2, b2, a3, b3, a4, b4;
  38. int    op;
  39. {
  40.     float    x1, y1, x2, y2, x3, y3, x4, y4;
  41.     float    xmid, ymid;
  42.  
  43.     clear_stack();
  44.     push(a1, b1, a2, b2, a3, b3, a4, b4);
  45.  
  46.     while(pop(&x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4)) {
  47.         xmid = half(x2, x3);
  48.         ymid = half(y2, y3);
  49.         if (fabs(x1 - xmid) < THRESHOLD && fabs(y1 - ymid) < THRESHOLD) {
  50.         pw_vector(canvas_pixwin, round(x1), round(y1), 
  51.             round(xmid), round(ymid), op, 1);
  52.         }
  53.         else {
  54.         push(x1, y1, half(x1, x2), half(y1, y2),
  55.             half(x2, xmid), half(y2, ymid), xmid, ymid);
  56.         }
  57.  
  58.         if (fabs(xmid - x4) < THRESHOLD && fabs(ymid - y4) < THRESHOLD) {
  59.         pw_vector(canvas_pixwin, round(xmid), round(ymid), 
  60.             round(x4), round(y4), op, 1);
  61.         }
  62.         else {
  63.         push(xmid, ymid, half(xmid, x3), half(ymid, y3),
  64.             half(x3, x4), half(y3, y4), x4, y4);
  65.         }
  66.         }
  67.     }
  68.  
  69. bezier_spline(a0, b0, a1, b1, a2, b2, a3, b3, op)
  70. float    a0, b0, a1, b1, a2, b2, a3, b3;
  71. int    op;
  72. {
  73.     float    x0, y0, x1, y1, x2, y2, x3, y3;
  74.     float    sx1, sy1, sx2, sy2, tx, ty, tx1, ty1, tx2, ty2, xmid, ymid;
  75.  
  76.     clear_stack();
  77.     push(a0, b0, a1, b1, a2, b2, a3, b3);
  78.  
  79.     while(pop(&x0, &y0, &x1, &y1, &x2, &y2, &x3, &y3)) {
  80.         if (fabs(x0 - x3) < THRESHOLD && fabs(y0 - y3) < THRESHOLD) {
  81.         pw_vector(canvas_pixwin, round(x0), round(y0), 
  82.             round(x3), round(y3), op, 1);
  83.         }
  84.         else {
  85.         tx = half(x1, x2);    ty = half(y1, y2);
  86.         sx1 = half(x0, x1);    sy1 = half(y0, y1);
  87.         sx2 = half(sx1, tx);    sy2 = half(sy1, ty);
  88.         tx2 = half(x2, x3);    ty2 = half(y2, y3);
  89.         tx1 = half(tx2, tx);    ty1 = half(ty2, ty);
  90.         xmid = half(sx2, tx1);    ymid = half(sy2, ty1);
  91.  
  92.         push(x0, y0, sx1, sy1, sx2, sy2, xmid, ymid);
  93.         push(xmid, ymid, tx1, ty1, tx2, ty2, x3, y3);
  94.         }
  95.         }
  96.     }
  97.